package com.pioneer.admin.controller.book;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.book.domain.Category;
import com.pioneer.web.book.service.ICategoryService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分类
 *
 * @author xx
 */
@RestController
@RequestMapping("/library/category")
public class CategoryController extends BaseController {

    @Resource
    private ICategoryService categoryService;

    /**
     * 获取分类列表
     *
     * @param category 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:category:list')")
    @GetMapping("/list")
    public AjaxResult list(Category category) {
        boolean isPage = startPage();
        List<Category> list = categoryService.list(Wrappers.query(category));
        return success(isPage, list);
    }

    /**
     * 根据分类id获取详细信息
     *
     * @param categoryId 分类id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable Long categoryId) {
        return AjaxResult.success(categoryService.getById(categoryId));
    }

    /**
     * 新增分类
     *
     * @param category 分类
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:category:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Category category) {
        return toAjax(categoryService.save(category));
    }

    /**
     * 修改分类
     *
     * @param category 分类
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:category:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Category category) {
        return toAjax(categoryService.updateById(category));
    }

    /**
     * 删除分类
     *
     * @param categoryId 分类id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:category:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable Long categoryId) {
        return toAjax(categoryService.deleteById(categoryId));
    }
}
